#include <string>

#include <iostream>

#include <fstream>

#include "display.h"

int i,j,k;

/************************************************************************/
Display::Display()
{
	out.open("output.html");
	ifstream in("input.txt");
	for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				if(!in.eof())
					displayArray[i][j]=in.get();
					
				else 
					displayArray[i][j]=' ';
	
			}

		}

	for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				
					formatPrefix[i][j]=" ";
	
			}

		}

		for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				
					formatSuffix[i][j]=" ";
	
			}

		}

		for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				
					colorFormatPrefix[i][j]=" ";
	
			}

		}

		for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				
					colorFormatSuffix[i][j]=" ";
	
			}

		}
	in.close();
}

/************************************************************************/

void Display::ChangeCharacter()
{
	int index1=0;
	int index2=0;
	char replacement=' ';
	cout<<endl<<"What row is the character in? ";
	cin>>index1;
	cout<<endl<<"What column is the character in? ";
	cin>>index2;
	cout<<endl<<"What would you like to replace the character with? ";
	cin>>replacement;
	displayArray[index1][index2]=replacement;

}

/************************************************************************/

void Display::ChangeWord()
{
	int index1=0;

	int index2=0;

	int tempIndex1=0;

	int tempIndex2=0;

	int originalIndex1=0;

	int originalIndex2=0;

	int i=0;

	int count=0;

	string replacement=" ";

	int wordLength=0;

	int replacementLength=0;

	int originalLength=0;

	int lengthDifference=0;

	char current=' ';

	char tempArray[8][60];


	
	cout<<endl<<"What row is the word in? ";

	cin>>index1;

	tempIndex1=index1;
	originalIndex1=index1;

	cout<<endl<<"What column does the word start in? ";

	cin>>index2;

	tempIndex2=index2;
	originalIndex2=index2;

	cout<<endl<<"What would you like to replace the word with? ";

	cin>>replacement;

	replacementLength=replacement.length();

	
	current=displayArray[tempIndex1][tempIndex2];

	count++;

	tempIndex2++;

	current=displayArray[tempIndex1][tempIndex2];

	while(current!=' ' && current!='.' && current!=',' && current!='?')

	{
		if(tempIndex2>59)

		{
			tempIndex2=0;

			tempIndex1++;

		}
		
		current=displayArray[tempIndex1][tempIndex2];

		count++;

		tempIndex2++;


	}

	count--;

	lengthDifference=replacement.length()-count;

	tempIndex1=index1;

	tempIndex2=index2;

	if(lengthDifference>0)
	{
		for(i=0;i<8;i++)
		{
			for(j=0;j<60;j++)
			{
				tempArray[i][j]=displayArray[i][j];
			}
		}

		
	for(i=0;i<replacementLength;i++)
		{
			if(tempIndex2+i>59)
			{
				tempIndex2=0;
				tempIndex1++;
			}
			displayArray[tempIndex1][tempIndex2]=replacement[i];
			tempIndex2++;
		}


		tempIndex1=index1;
		tempIndex2=index2+replacementLength;

	while(tempIndex1!=7)
	{

		if(tempIndex2>59)
			{
				tempIndex2=0;
				tempIndex1++;
			}

		if(index2>59)
			{
				originalIndex2=0;
				originalIndex1++;
			}
		displayArray[tempIndex1][tempIndex2]=tempArray[originalIndex1][originalIndex2+(replacementLength-lengthDifference)];

		originalIndex2++;
		tempIndex2++;


	}
	
	}
//////////////////////////////////////////////
	else if(lengthDifference<0)
	{
	tempIndex1=index1;
	originalIndex1=index1;
	tempIndex2=index2;
	originalIndex2=index2;
		for(i=0;i<8;i++)
		{
			for(j=0;j<60;j++)
			{
				tempArray[i][j]=displayArray[i][j];
			}
		}

		
		for(i=0;i<replacementLength;i++)
		{
			if(tempIndex2+i>59)
			{
				tempIndex2=0;
				tempIndex1++;
			}
			displayArray[tempIndex1][tempIndex2]=replacement[i];
			tempIndex2++;
		}




tempIndex1=index1;
		tempIndex2=index2+replacementLength;

	while(tempIndex1!=7)
	{

		if(tempIndex2>59)
			{
				tempIndex2=0;
				tempIndex1++;
			}

		if(index2>59)
			{
				originalIndex2=0;
				originalIndex1++;
			}
		displayArray[tempIndex1][tempIndex2]=tempArray[originalIndex1][originalIndex2+(replacementLength-lengthDifference)];

		originalIndex2++;
		tempIndex2++;


	}



}


	else

		for(i=0;i<replacementLength;i++)
		{
			if(index2+i>59)
			{
				index2=0;
				index1++;
			}
			displayArray[index1][index2]=replacement[i];
			index2++;
		}
	
}

/************************************************************************/

void Display::DrawArray()
{
cout<<endl;
cout<<"       0         10        20        30        40        50    "<<endl;
		cout<<"  012345678901234567890123456789012345678901234567890123456789"<<endl;
		for(i=0;i<8;i++)
		{
			cout<<i<<' ';
			for(j=0;j<60;j++)
			{
				cout<<displayArray[i][j];
			}
			cout<<endl;
		}

}

/************************************************************************/


void Display::ChangeFormat()
{


	int index1=0;
	int index2=0;
	char replacement=' ';
	int count=0;
	int tempIndex1=0;
	int tempIndex2=0;
	char current=' ';
	cout << " What row is the word in? ";

	cin >> index1;

	cout << endl << " What column is the word in? ";

	cin >> index2;

	cout << endl;

	system("cls");

	cout << "How would you like to format the word? " << endl << endl;

	cout << "Change word to bold.			(b)" << endl;

	cout << "Change word to italicized.		(i)" << endl;

	cout << "Change word to underline		(u)" << endl;

	cout << "Change color of word			(c)" << endl;

	cin >> replacement;

	if (replacement== 'b' || replacement == 'B')

		{

			tempIndex1=index1;
			tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					formatPrefix[tempIndex1][tempIndex2]="<b>";
					formatSuffix[tempIndex1][tempIndex2]="</b>";

				count++;

				tempIndex2++;


			}

			

		}

	if (replacement== 'i' || replacement == 'I')

		{
			tempIndex1=index1;
			tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					formatPrefix[tempIndex1][tempIndex2]="<i>";
					formatSuffix[tempIndex1][tempIndex2]="</i>";

				count++;

				tempIndex2++;


			}

		}

	if (replacement== 'u' || replacement == 'U')

		{
			tempIndex1=index1;
			tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					formatPrefix[tempIndex1][tempIndex2]="<u>";
					formatSuffix[tempIndex1][tempIndex2]="</u>";

				count++;

				tempIndex2++;


			}

		}

	if (replacement== 'c' || replacement == 'C')

		{
			cout << " What color would you like to change the text to ? " << endl;

			cout <<	" Red			(r)" << endl;

			cout << " Blue			(b)" << endl;

			cout << " Yellow			(y)" << endl;

			cout << " Green			(g)" << endl;

			cout << " Orange			(o)" << endl;

			cout << " White			(w)" << endl;

			cout << " Purple			(p)" << endl;

			cin >> replacement;

		if (replacement =='r' || replacement == 'R')

			{

				
			tempIndex1=index1;
			tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#FF0000>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;


			}

		
			

	}


		if (replacement =='b' || replacement == 'B')



			{

				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#0000FF>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;


			}

		}


		if (replacement =='y' || replacement == 'Y')

			{

			
				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#FFFF00>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;



			}

		}



		if (replacement =='g' || replacement == 'G')

			{

			
				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#00FF00>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;



			}


		}



		if (replacement =='o' || replacement == 'O')

			{

			
				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#FF8040>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;



			}

			
	
		}


		if (replacement =='w' || replacement == 'W')

			{

			
				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#FFFFFF>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;



			}
	

		}


		if (replacement =='p' || replacement == 'P')

			{

			
				tempIndex1=index1;
				tempIndex2=index2;


			current=displayArray[tempIndex1][tempIndex2];
	
			while(current!=' ' && current!='.' && current!=',' && current!='?')
	
			{
				current=displayArray[tempIndex1][tempIndex2];

				if(tempIndex2>59)
	
				{
					tempIndex2=0;
	
					tempIndex1++;

				}
		
					colorFormatPrefix[tempIndex1][tempIndex2]="<font color=#800080>";
					colorFormatSuffix[tempIndex1][tempIndex2]="</font>";

				count++;

				tempIndex2++;



			}
	
			

		}
	
	

	}
	

	

}

/************************************************************************/

void Display::DrawOutputFile()
{
	
	ofstream out("output.html");
	ofstream out2("output.jsd");

	for(i=0;i<8;i++)
		{
	
			for(j=0;j<60;j++)
			{
				
				//if(displayArray[i][j]==' ')
				//	out<<' ';
				if(colorFormatPrefix[i][j]!=" ")
					out<<colorFormatPrefix[i][j];
				if(formatPrefix[i][j]!=" ")
					out<<formatPrefix[i][j];
				out<<displayArray[i][j];
				if(formatSuffix[i][j]!=" ")
					out<<formatSuffix[i][j];
				if(colorFormatSuffix[i][j]!=" ")
					out<<colorFormatSuffix[i][j];
			
			}
			
			//out<<endl;
		}

	for(i=0;i<8;i++)
		{
	
			for(j=0;j<60;j++)
			{
				
			
				if(colorFormatPrefix[i][j]!=" ")
					out2<<colorFormatPrefix[i][j];
				if(formatPrefix[i][j]!=" ")
					out2<<formatPrefix[i][j];
				out2<<displayArray[i][j];
				if(formatSuffix[i][j]!=" ")
					out2<<formatSuffix[i][j];
				if(colorFormatSuffix[i][j]!=" ")
					out2<<colorFormatSuffix[i][j];
				
			}
			
			out2<<endl;
		}

}

/************************************************************************/

void Display::DeleteText()
{
/*
	int startRow=0;
	int startCol=0;
	int endRow=0;
	int endCol=0;
	int deleteLength=0;
	int tempRow=0;
	int tempCol=0;
	int index1=0;
	int index2=0;
	int tempIndex1=0;
	int tempIndex2=0;
	int originalIndex1=0;
	int originalIndex2=0;
	char tempArray[8][60];

	cout<<endl<<"At which row would you like to begin deleting? ";
	cin>>startRow;
	cout<<endl<<"At which column would you like to begin deleting? ";
	cin>>startCol;
	cout<<endl<<"At which row would you like to stop deleting? ";
	cin>>endRow;
	cout<<endl<<"At which column would you like to stop deleting? ";
	cin>>endCol;
	tempRow=startRow;
	tempCol=startCol;
	index1=startRow;
	index2=startCol;
	while(tempRow!=endRow && tempCol!=endCol)
	{
		if(tempCol+i>59)
			{
				tempCol=0;
				tempRow++;
			}
			
			tempCol++;
			deleteLength++;
	}




	tempIndex1=index1;
	originalIndex1=index1;
	tempIndex2=index2;
	originalIndex2=index2;
		for(i=0;i<8;i++)
		{
			for(j=0;j<60;j++)
			{
				tempArray[i][j]=displayArray[i][j];
			}
		}

		

	while(tempIndex1!=7)
	{

		if(tempIndex2>59)
			{
				tempIndex2=0;
				tempIndex1++;
			}

		if(index2>59)
			{
				originalIndex2=0;
				originalIndex1++;
			}
		displayArray[tempIndex1][tempIndex2]=tempArray[originalIndex1][originalIndex2+(deleteLength)];

		originalIndex2++;
		tempIndex2++;

	}


	*/

}


/************************************************************************/

void Display::AddText()
{
	int index1=0;

	int index2=0;

	int tempIndex1=0;

	int tempIndex2=0;

	int originalIndex1=0;

	int originalIndex2=0;

	int count=0;

	string replacement=" ";

	int replacementLength=0;

	char current=' ';

	char tempArray[8][60];

	cout<<endl<<"At which row would you like to add text? "<<endl;
	cin>>index1;
	cout<<endl<<"At which column would you like to add text? "<<endl;
	cin>>index2;
	cout<<endl<<"What would you like to insert? "<<endl;
	cin>>replacement;
	tempIndex1=index1;
	originalIndex1=index1;
	tempIndex2=index2;
	originalIndex2=index2;

	replacementLength=replacement.length();

	for(i=0;i<8;i++)
			{
				for(j=0;j<60;j++)
				{
					tempArray[i][j]=displayArray[i][j];
				}
			}

		
		for(i=0;i<replacementLength;i++)
			{
				if(tempIndex2+i>59)
				{
					tempIndex2=0;
					tempIndex1++;
				}
				displayArray[tempIndex1][tempIndex2]=replacement[i];
				tempIndex2++;
			}
	

			tempIndex1=index1;
			tempIndex2=index2+replacementLength;

		while(tempIndex1!=7)
		{
	
			if(tempIndex2>59)
				{
					tempIndex2=0;
					tempIndex1++;
				}
	
			if(index2>59)
				{
					originalIndex2=0;
					originalIndex1++;
				}
			displayArray[tempIndex1][tempIndex2]=tempArray[originalIndex1][originalIndex2];
	
			originalIndex2++;
			tempIndex2++;
	
	
		}

}


/************************************************************************/

void Display::CloseFile()
{
	out.close();

}

/************************************************************************/

void Display::SaveFile()
{
	ofstream out3("input.txt");
	for(i=0;i<8;i++)
		{
			
			for(j=0;j<60;j++)
			{
				
					out3<<displayArray[i][j];
	
			}

		}
	out.close();
}

/************************************************************************/

Display::~Display()
{

}